package com.ampaiva.app;

import java.lang.reflect.Method;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomArrayAdapter<T> extends ArrayAdapter<T> {

    private final int rowViewResourceId;
    private final Class<?> Ridclass;

    public CustomArrayAdapter(Context context, Class<?> Rclass, int textViewResourceId, List<T> objects) {
        super(context, textViewResourceId, objects);
        this.Ridclass = Rclass;
        this.rowViewResourceId = textViewResourceId;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rowView = inflater.inflate(rowViewResourceId, parent, false);

        T row = getItem(position);

        setFields(rowView, row);

        return rowView;
    }

    private void setFields(View rowView, T row) {
        Method[] m = row.getClass().getMethods();
        for (Method method : m) {
            int id2 = ResourceUtil.getResourceByName(Ridclass, row.getClass().getSimpleName().toLowerCase() + "_" + method.getName().toLowerCase());
            if (id2 != -1) {
                setValue(rowView, row, method, id2);
                continue;
            }
            if (!method.getName().startsWith("get"))
                continue;
            String fieldName = method.getName().toLowerCase().substring(3);
            int id = ResourceUtil.getResourceByName(Ridclass, row.getClass().getSimpleName().toLowerCase() + "_" + fieldName);
            if (id == -1)
                continue;
            setValue(rowView, row, method, id);
        }
    }

    private void setValue(View rowView, T row, Method method, int id) {
        View view = rowView.findViewById(id);
        setValuebyViewType(row, method, view);
    }

    private void setValuebyViewType(T row, Method method, View view) {
        if (view == null)
            return;

        if (view instanceof TextView) {
            TextView textView = (TextView) view;
            if (method.getReturnType().isAssignableFrom(String.class)) {
                String value = getString(row, method);
                textView.setText(value);
            }
        } else if (view instanceof ImageView) {
            ImageView imageView = (ImageView) view;
            if (method.getReturnType().isAssignableFrom(Bitmap.class)) {
                Bitmap value = getBitmap(row, method);
                imageView.setImageBitmap(value);
            }
        }
    }

    private String getString(T row, Method method) {
        try {
            return (String) method.invoke(row);
        } catch (Exception e) {
            Log.w("getString: ", e.toString());
        }
        return null;
    }

    private Bitmap getBitmap(T row, Method method) {
        try {
            return (Bitmap) method.invoke(row);
        } catch (Exception e) {
            Log.w("getBitmap: ", e.toString());
        }
        return null;
    }

}
